package org.example.forum.controller;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.model.Board;
import org.example.forum.service.IBoradService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/board")
@Slf4j
public class BoradController {
    @Resource
    private IBoradService boradService;

    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;
   @PostMapping ("/create")
    public AppResult createBorad(@RequestParam(value = "name")  @Nonnull String name,
                                 @RequestParam(value = "sort") @Nonnull Integer sort) {
        Board board = new Board();
        board.setName(name);
        board.setSort(sort);
        boradService.create(board);
        return AppResult.success();
    }
    @GetMapping ("/getById")
    public  AppResult<Board> getBoard(@RequestParam(value = "id") @Nonnull Long id) {
        Board result=boradService.selectById(id);
        return AppResult.success(result);
    }
    @GetMapping("/topList")
    public AppResult<List<Board>> topList () {
        System.out.println("indexBoardNum = " + indexBoardNum);
        //调⽤Service层获取版块信息

        List<Board> boards = boradService.selectByNum(indexBoardNum);
        //返回结果

        return AppResult.success(boards);
         }
    @GetMapping("/allNormal")
    public AppResult<List<Board>> allNormal () {
        //调⽤Service层获取版块信息
        List<Board> boards = boradService.selectAllNormal();
        //返回结果
        return AppResult.success(boards);
    }

    }
